package com.ling.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.bean.BlogComment;
import com.ling.bean.Msg;
import com.ling.service.BlogCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class BlogCommentController {
    private final BlogCommentService blogCommentService;

    @Autowired
    public BlogCommentController(BlogCommentService blogCommentService) {
        this.blogCommentService = blogCommentService;
    }

    //跳转到查看用户博客评论页面
    @RequestMapping(value = "/blog-comment", method = RequestMethod.GET)
    public String goToBlogComment() {
        return "/blog-comment";
    }


    //获取用户所有的博客评论,按照时间逆序排列，分页
    @RequestMapping(value = "/getMyBlogComment/{userId}/{pn}", method = RequestMethod.GET)
    @ResponseBody
    public Msg getMyBlogComment(@PathVariable String userId, @PathVariable Integer pn) {
        //开始分页
        PageHelper.startPage(pn, 10);
        //查询数据
        List<BlogComment> comments = blogCommentService.getBlogCommentByUser(userId);
        //封装分页数据
        PageInfo<BlogComment> pageInfo = new PageInfo<>(comments);
        return Msg.success().add("pageInfo", pageInfo);
    }

}
